<?php
class IndexAction extends ZMAction {
    public function index(){
        $app_url = dirname(__APP__);
        $app_url = str_replace("\\","/",$app_url);
        if(substr($app_url, -1, 1)!='/') $app_url .= '/';
        $this->assign('app_url',$app_url);
        if(!isset($_SESSION['user'])){
            $this->assign('cur_user','null');
        }else{
            $this->assign('cur_user',json_encode($_SESSION['user']));
        }        
        if($_SESSION['isAdmin']){
            $this->assign('isAdmin','true');
        }else{
            $this->assign('isAdmin','false');
        }
        $this->display('Index');
    }
    
    public function login(){
        $UserInfo = M('User');
        $where['loginname']=$_POST['username'];
        $where['passwd']=md5($_POST['passwd']);
        $res = $UserInfo->where($where)->find();
        if($res){
            unset($res['passwd']);
            $_SESSION['user'] = $res;
            $this->ajaxReturn($res,'登录成功。',1);
        }else{
            $this->error('用户名或者密码错误。');
        }
    }
    
    public function logout(){
        session_destroy();
        $app_url = dirname(__APP__);
        if(substr($app_url, -1, 1)!='/') $app_url .= '/';
        $this->assign('app_url',$app_url);
        $this->assign("login","false");
        $this->display('Logout');
    }
    
    public function NavTree(){
        $user = $_SESSION['user'];
        if(!isset($_SESSION['user'])) return;
        $data = array();

        //机房管理
        $btn['id'] = 'room';
        $btn['text'] = '虚拟机房管理';
        $btn['iconCls'] = 'room';
        $btn['expanded'] = true;

        $childs = array();

        $child['id'] = 'x3dcontainer';
        $child['text'] = '3D设备浏览';
        $child['iconCls'] = 'x3dview';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'line';
        $child['text'] = '设备线缆管理';
        $child['iconCls'] = 'line';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'tour';
        $child['text'] = '机房设备巡视';
        $child['iconCls'] = 'tour';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'totalview';
        $child['text'] = '实时数据查看';
        $child['iconCls'] = 'totalview';
        $child['leaf'] = true;
        $childs[] = $child;

        $btn['children'] = $childs;
        $data[] = $btn;

        //统计报表
        $btn['id'] = 'report';
        $btn['text'] = '数据统计查询';
        $btn['iconCls'] = 'report';
        $btn['expanded'] = true;

        $childs = array();

        $child['id'] = 'linesearch';
        $child['text'] = '线缆数据查询';
        $child['iconCls'] = 'linesearch';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'tourlog';
        $child['text'] = '设备巡视记录查询';
        $child['iconCls'] = 'tourlog';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'historylist';
        $child['text'] = '历史数据查询';
        $child['iconCls'] = 'historylist';
        $child['leaf'] = true;
        $childs[] = $child;
        
        $model = M('Chart');
        $res = $model->select();
        foreach($res as $row){
            $child['id'] = 'chartdata'.$row['cid'];
            $child['text'] = $row['chartname'];
            $child['iconCls'] = 'chart';
            $child['leaf'] = true;
            $childs[] = $child;            
        }
/*
        $child['id'] = 'uisearch';
        $child['text'] = '动力负荷曲线';
        $child['iconCls'] = 'uisearch';
        $child['leaf'] = true;
        $childs[] = $child;


        $child['id'] = 'devsearch';
        $child['text'] = '设备统计报表';
        $child['iconCls'] = 'devtotal';
        $child['leaf'] = true;
        $childs[] = $child;
 */
        $btn['children'] = $childs;
        $data[] = $btn;
 /* */
        //设备定义设置
        /*
        $btn['id'] = 'devdef';
        $btn['text'] = '设备定义设置';
        $btn['iconCls'] = 'devsetup';
        $btn['expanded'] = true;

        $childs = array();

        $child['id'] = 'modelview';
        $child['text'] = '设备模型定义';
        $child['iconCls'] = 'devdef';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'devman';
        $child['text'] = '设备数据管理';
        $child['iconCls'] = 'devman';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'coldef';
        $child['text'] = '数据采集定义';
        $child['iconCls'] = 'coldef';
        $child['leaf'] = true;
        $childs[] = $child;


        $child['id'] = 'tourlist';
        $child['text'] = '巡视线路设置';
        $child['iconCls'] = 'tourlist';
        $child['leaf'] = true;
        $childs[] = $child;

        $btn['children'] = $childs;
        $data[] = $btn;
*/
    if($_SESSION['user']['isadmin']==1){
        //系统设置
        $btn['id'] = 'setup';
        $btn['text'] = '系统设置';
        $btn['iconCls'] = 'setup';
        $btn['expanded'] = true;

        $childs = array();

        $child['id'] = 'modelview';
        $child['text'] = '设备模型定义';
        $child['iconCls'] = 'devdef';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'tourlist';
        $child['text'] = '巡视线路设置';
        $child['iconCls'] = 'tourlist';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'chartlist';
        $child['text'] = '曲线图表定义';
        $child['iconCls'] = 'chartlist';
        $child['leaf'] = true;
        $childs[] = $child;

        $child['id'] = 'userlist';
        $child['text'] = '用户设置';
        $child['iconCls'] = 'userlist';
        $child['leaf'] = true;
        $childs[] = $child;

        $btn['children'] = $childs;
        $data[] = $btn;
    }        

        $child['id'] = 'logout';
        $child['text'] = '登出系统';
        $child['iconCls'] = 'logout';
        $child['leaf'] = true;
        
        $data[] = $child;
        
        echo(json_encode($data));
    }
    
}